package com.chat.system.controller;

import com.chat.system.entity.domain.Res;
import com.chat.system.entity.dto.ChatRecordDTO;
import com.chat.system.entity.dto.ChatUserDTO;
import com.chat.system.entity.dto.LatelyChatFriendDTO;
import com.chat.system.entity.po.ChatUser;
import com.chat.system.service.IChatUserRelationshipService;
import com.chat.system.service.IChatUserService;
import com.chat.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czh
 * @since 2023-10-27
 */
@RestController
@RequestMapping("/chatUser")
public class ChatUserController {

    @Autowired
    IChatUserService chatUserService;
    @Autowired
    IChatUserRelationshipService iChatUserRelationshipService;

    @GetMapping("listByName")
    public Res listByName(String userName) {
        if (userName.equals("")){
            return Res.success();
        }
        final String currentUserName = UserUtil.getCurrentUserName();
        final List<ChatUserDTO> list = chatUserService.listChatUsers("%" + userName + "%",currentUserName);
        final Res success = Res.success();
        success.put("list",list);
        return success;
    }

    @GetMapping("getByUserName")
    public Res getByUserName(String userName) {
        final ChatUser chatUser = chatUserService.getById(userName);
        ChatUserDTO chatUserDTO = new ChatUserDTO();
        chatUserDTO.setUserName(chatUser.getUserName());
        final Res success = Res.success();
        success.put("user",chatUserDTO);
        return success;
    }

    @PostMapping("beginNewChat")
    public Res beginNewChat (@RequestBody Map<String, String> params) {
        String relationshipId = params.get("relationshipId");
        chatUserService.beginNewChat(relationshipId,"");
        return Res.success();
    }

    @GetMapping("getLatelyChat")
    public Res getLatelyChat () {
        String currentUserName = UserUtil.getCurrentUserName();
        final List<LatelyChatFriendDTO> friendList = chatUserService.getLatelyFriendList(currentUserName);
        final Res success = Res.success();
        success.put("list",friendList);
        return success;
    }
    @GetMapping("getLatelyChatRecords")
    public Res getLatelyChatRecords() {
        String currentUserName = UserUtil.getCurrentUserName();
        Map<String, Map<String, Object>> records = chatUserService.getLatelyChatRecords(currentUserName);
        final Res success = Res.success();
        success.put("records_totalNumberOfRecords",records);
        return success;
    }
    @GetMapping("fetchChatRecords")
    public Res fetchChatRecords(String friendShipId,Long current,Long total) {
        System.out.println(friendShipId + "->" + current + "->" +total);
        List<ChatRecordDTO> chatRecordDTOS =  chatUserService.fetchChatRecords(friendShipId,current,total);
        final Res success = Res.success();
        success.put("records",chatRecordDTOS);
        return success;
    }
    @PostMapping("sendMessage")
    public Res sendMessage(@RequestBody ChatRecordDTO chatRecordDTO) {
        chatUserService.sendMessage(chatRecordDTO);
        return Res.success();
    }
    @GetMapping("checkOnline")
    public Res checkOnline(String friendName) {
        boolean isOnline = chatUserService.checkOnline(friendName);
        final Res success = Res.success();
        success.put("isOnline",isOnline);
        return success;
    }

    @PostMapping("videoInvite")
    public Res videoInvite (@RequestBody Map<String, String> params) {
        String friendName = params.get("friendName");
        chatUserService.videoInvite(friendName);
        return Res.success();
    }
    @PostMapping("videoInviteConfirm")
    public Res videoInviteConfirm (@RequestBody Map<String, Object> params) {
        boolean confirm = (boolean) params.get("confirm");
        String friendName = (String) params.get("friendName");
        chatUserService.videoInviteConfirm(friendName,confirm);
        return Res.success();
    }

    @GetMapping("stopVideo")
    public Res stopVideo(String friendName) {
        chatUserService.stopVideo(friendName);
        return Res.success();
    }

    @PostMapping("preHandleIMG")
    public Res preHandleIMG (@RequestBody ChatRecordDTO chatRecordDTO) {
        ChatRecordDTO chatRecord =  chatUserService.preHandleIMG(chatRecordDTO);
        final Res success = Res.success();
        success.put("chatRecord",chatRecord);
        return success;
    }
}
